package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.enums.boss.AmibaCode;
import com.zhongfl.crm.lixiao.mapper.AmibaMapper;
import com.zhongfl.crm.service.boss.AmibaService;
import com.zhongfl.crm.service.boss.AmibaStaffService;
import com.zhongfl.crm.model.boss.Amiba;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wang.yuchuan
 * @date 2018/5/3
 */
@Service
public class AmibaServiceImpl implements AmibaService {

    @Resource
    private AmibaMapper amibaMapper;
    @Resource
    private AmibaStaffService amibaStaffService;

    @Override
    public Amiba findByStaffAmibaId(Integer amibaId) {

        return amibaMapper.findByStaffAmibaId(amibaId);
    }

    @Override
    public Integer saveAmiba(Amiba amiba) {
        AssertUtils.notNull(amiba, "要创建的阿米巴为Null");
        AssertUtils.hasText(amiba.getName(), "要创建的阿米巴名称为空");
        AssertUtils.hasText(amiba.getCreatedBy(), "阿米巴创建人不允许为空");
        if (StringUtils.isEmpty(amiba.getModifiedBy())) {
            amiba.setModifiedBy(amiba.getCreatedBy());
        }
        if (Objects.isNull(amiba.getParentId())) {
            if (Objects.isNull(amiba.getCode())) {
                amiba.setCode(AmibaCode.OTHER);
            }
        } else {
            Amiba parentAmiba = amibaMapper.selectById(amiba.getParentId());
            if (Objects.nonNull(parentAmiba)) {
                amiba.setCode(parentAmiba.getCode());
            } else {
                amiba.setCode(AmibaCode.OTHER);
            }
        }

        amibaMapper.insertSelective(amiba);
        return amiba.getId();
    }

    @Override
    public void updateAmiba(Amiba amiba) {
        AssertUtils.notNull(amiba, "要修改的阿米巴为Null");
        AssertUtils.greaterThanZero(amiba.getId(), "要修改的阿米巴ID必须大于0");
        AssertUtils.hasText(amiba.getModifiedBy(), "阿米巴修改人不允许为空");

        amibaMapper.updateByPrimaryKeySelective(amiba);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAmiba(Integer id) {
        if (Objects.isNull(id) || id <= 0) {
            return;
        }

        //批量删除子巴
        List<Amiba> childrenAmiba = this.findByParentId(id);
        if(Objects.isNull(childrenAmiba) || childrenAmiba.size() == 0){
            amibaMapper.deleteByPrimaryKey(id);
            amibaStaffService.deleteByAmibaId(id);
        }else{
            List<Integer> amibaIds = childrenAmiba.stream()
                    .map(Amiba::getId)
                    .collect(Collectors.toList());
            amibaIds.add(id);

            amibaMapper.deleteBatch(amibaIds);

            //删除阿米巴员工与被删除的阿米巴的关系
            amibaStaffService.deleteBatchByAmibaId(amibaIds);
        }
    }

    @Override
    public List<Amiba> findAll() {
        return amibaMapper.selectAll();
    }

    @Override
    public Amiba findById(Integer id) {
        return amibaMapper.selectById(id);
    }

    @Override
    public List<Amiba> findByCode(String code) {
        return amibaMapper.selectByCode(code);
    }

    @Override
    public List<Amiba> findByParentId(Integer parentId) {
        return amibaMapper.selectByParentId(parentId);
    }

    @Override
    public List<Amiba> findAmibaList(Integer amibaId) {
        Amiba amiba = this.findById(amibaId);
        if (amiba != null) {
            AmibaCode code = amiba.getCode();
            List<Amiba> amibas = this.findByCode(code.name());
            return amibas;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 查询所有的阿米巴
     *
     * @return
     */
    @Override
    public List<Amiba> findAllamiba() {
        return amibaMapper.selectAll();
    }
}
